<template>
  <div>
    <el-dialog title="核心客户选择" :visible.sync="dialogVisible" width="80%">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户行业" prop="customerIndustry">
          <el-select
            v-model="queryParams.customerIndustry"
            placeholder="请选择客户行业"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_industry"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户级别" prop="customerRank">
          <el-select
            v-model="queryParams.customerRank"
            placeholder="请选择客户级别"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_rank"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户类型" prop="customerType">
          <el-select
            v-model="queryParams.customerType"
            placeholder="请选择客户类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.crm_customer_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="customerList"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户名称" align="center" prop="customerName" />
        <el-table-column label="联系人" align="center" prop="linkman" />
        <el-table-column label="职务" align="center" prop="duty" />
        <el-table-column label="联系电话" align="center" prop="phone" />
        <el-table-column label="地区" align="center" prop="region" />
        <el-table-column
          label="客户行业"
          align="center"
          prop="customerIndustry"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.customer_industry"
              :value="scope.row.customerIndustry"
            />
          </template>
        </el-table-column>
        <el-table-column label="客户级别" align="center" prop="customerRank">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.customer_rank"
              :value="scope.row.customerRank"
            />
          </template>
        </el-table-column>
        <el-table-column label="客户类型" align="center" prop="customerType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.crm_customer_type"
              :value="scope.row.customerType"
            />
          </template>
        </el-table-column>
        <el-table-column label="客户状态" align="center" prop="customerStatus">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.customer_status"
              :value="scope.row.customerStatus"
            />
          </template>
        </el-table-column>
        <el-table-column label="归属人" align="center" prop="createBy" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="chooseCustomerInfo(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomer,
  chooseCustomer,
  getList
} from "@/api/crm/customer";

export default {
  name: "Customer",
  dicts: [
    "customer_industry",
    "crm_customer_type",
    "customer_rank",
    "customer_status",
  ],
  data() {
    return {
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户列表格数据
      customerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        linkman: null,
        phone: null,
        regionId: null,
        customerIndustry: null,
        customerRank: null,
        customerType: null,
        customerLabel: null,
        customerStatus: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    chooseCustomerInfo(row){
        chooseCustomer(row).then(res => {
            this.$message.success("选择成功")
            this.getList()
        })
    },
    open() {
      this.dialogVisible = true;
    },
    /** 查询客户列列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then((response) => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
